/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package com.pearreview.ui;

import java.awt.Color;
import java.awt.Font;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;

public class DurationSpinner extends JSpinner {
	private static final long serialVersionUID = -6168908896700606229L;
	
	private static final long TIMEZONE_OFFSET  = DateFormat
			.getDateTimeInstance().getCalendar().get(Calendar.ZONE_OFFSET);
	
	public DurationSpinner(long duration) {
		setFont(new Font("Tahoma", Font.PLAIN, 12));
		
		SpinnerModel model = new SpinnerDateModel(new Date(duration - TIMEZONE_OFFSET), 
				null, null, Calendar.DAY_OF_MONTH);
		setModel(model);
		
		DateEditor editor = new JSpinner.DateEditor(this, "H:mm");
		
		JFormattedTextField tf = editor.getTextField();
		CutCopyPasteManager.getInstance().registerJTextComponent(tf);
		FadingManager.getInstance().registerComponent(tf);
		setEditor(editor);
		tf.setHorizontalAlignment(JFormattedTextField.LEFT);
		
		if (System.getProperty("os.name").equals("Mac OS X")) {
			tf.setBorder(BorderFactory.createMatteBorder(
					1, 1, 1, 1, new Color(0x828790)));
		} else {
			setBorder(BorderFactory.createMatteBorder(
							1, 1, 1, 1, new Color(0x828790)));
		}
	}
	
	public DurationSpinner() {
		this(0);
	}
	
	
	public long getDuration() {
		return ((SpinnerDateModel)getModel()).getDate().getTime() 
				+ TIMEZONE_OFFSET;
	}
	
	public void setDuration(long duration) {
		((SpinnerDateModel)getModel()).setValue(
				new Date(duration - TIMEZONE_OFFSET));
	}
}
